﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microhard.WeixinMP.Interfaces;
using Microhard.Core;
using Microhard.WeixinMP.Models;

namespace Microhard.FMCG.WebUI.Areas.WX.Controllers
{
    public class ShowArticleController : Controller
    {
        private IArticleShareLink iasl = IoC.Resolve<IArticleShareLink>();
        private IArticle iar = IoC.Resolve<IArticle>();
        /// <summary>
        /// 通过微信菜单，查看文章列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(Guid companyID, string type, string openID)
        {
            IArticle iar = IoC.Resolve<IArticle>();
            var data = iar.ListPageView(new WeixinMP.Models.ArtileModel
            {
                CompanyID = companyID,
                Type = type,
                WeixinName = openID
            });
            return View(data);
        }

        /// <summary>
        /// 通过文章列表，查看文章明细
        /// </summary>
        /// <param name="nsid"></param>
        /// <param name="oid"></param>
        /// <param name="aid"></param>
        /// <returns></returns>
        public ActionResult Detial(string oid, Guid aid)
        {
            //此用户是否分享过
            var sharelink = iasl.GetModeByOAID(oid, aid);
            if (sharelink == null)
            {
                //新增
                var toadd = new ShareLinkModel
                {
                    ShareLinkID = Guid.NewGuid(),
                    ArticleID = aid,
                    ClickCount = 1,
                    CreateTime = DateTime.Now,
                    UpdateTime = DateTime.Now,
                    ShareUserOpenID = oid
                };
                iasl.Add(toadd);
            }
            else
            {
                sharelink.ClickCount = sharelink.ClickCount + 1;
                iasl.Update(sharelink);
            }
            var content = iar.GetModelByID(aid);
            return View("View", content);
        }
    }
}
